<?php defined('SYSPATH') or die('No direct script access.');
/**
 * 常用字符串处理
 *
 * @author  Yellow.Chow <aultoale@gmail.com>
 * @version $Id: str.php 157 2012-04-17 10:15:19Z yellow.chow $
 * @license http://www.gnu.org/licenses/gpl-3.0.html    GPL 3
 */
class Str
{

    /**
     * 字符串高亮
     *
     * @param  string         $string  需要的高亮的字符串
     * @param  string|array  $keyword  关键字
     * @return string
     */
    public static function highlight($string, $keyword)
    {
        $string = (string) $string;

        if ($string AND $keyword)
        {
            if ( ! is_array($keyword))
            {
                $keyword = array($keyword);
            }
            $pattern = array();
            foreach ($keyword as $word)
            {
                ! empty($word) AND $pattern[] = '(' . str_replace('/', '\/',  preg_quote($word)) . ')';
            }
            if ( ! empty($pattern))
            {
                $string = preg_replace(
                    '/(' . implode('|', $pattern) . ')/is',
                    '<span class="highlight">\\1</span>',
                    $string
                );
            }
        }

        return $string;
    }

    /**
     * 按指定的长度切割字符串
     *
     * @param  string   $string  需要切割的字符串
     * @param  integer  $length  长度
     * @param  string   $suffix  切割后补充的字符串
     * @return string
     */
    public static function slice($string, $length, $suffix = '')
    {
        $string = (string) $string;

        if (strlen($string) <= $length + strlen($suffix))
            return $string;

        $n = $tn = $noc = 0;
        while ($n < strlen($string))
        {
            $t = ord($string[$n]);
            if ($t == 9 || $t == 10 || (32 <= $t && $t <= 126))
            {
                $tn = 1; $n++; $noc++;
            }
            elseif (194 <= $t && $t <= 223)
            {
                $tn = 2; $n += 2; $noc += 2;
            }
            elseif (224 <= $t && $t < 239)
            {
                $tn = 3; $n += 3; $noc += 2;
            }
            elseif (240 <= $t && $t <= 247)
            {
                $tn = 4; $n += 4; $noc += 2;
            }
            elseif (248 <= $t && $t <= 251)
            {
                $tn = 5; $n += 5; $noc += 2;
            }
            elseif ($t == 252 || $t == 253)
            {
                $tn = 6; $n += 6; $noc += 2;
            }
            else
            {
                $n++;
            }
            if ($noc >= $length)
            {
                break;
            }
        }

        $noc > $length AND $n -= $tn;
        $slice = substr($string, 0, $n);
        if (strlen($slice) < strlen($string))
        {
            $slice .= $suffix;
        }

        return $slice;
    }

    /**
     * strip_tags 的修改版
     *
     * @param  string  $string
     * @return string
     */
    public static function strip_html($string)
    {
        $string = strip_tags(preg_replace('/(\s|&nbsp;|<br ?\/?>)+/i', ' ', (string) $string));
        return preg_replace('/[\s\x{3000}]+/u', ' ', $string);
    }

    /**
     * 获取中文字符长度(包括全角字符)
     *
     * @param  string  $str
     * @return int
     */
    public static function zh_strlen($str)
    {
        $str = preg_replace('/[\x{4E00}-\x{9FA5}\x{FE30}-\x{FFA0}\x{3000}-\x{3039}]/u', '**', $str);
        return UTF8::strlen($str);
    }

}